package com.zr.bookcorner.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;

import com.google.gson.Gson;
import com.zh.baselibrary.utils.PhoneUtil;
import com.zh.model.manager.QRCodeLoginInfo;
import com.zr.andealib.util.CardReaderUtil;
import com.zr.bookcorner.R;
import com.zr.bookcorner.utils.ZXingUtils;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ManagerVerificationDialog extends Dialog {
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.iv_code)
    ImageView ivCode;
    @BindView(R.id.ll_or)
    LinearLayout llOr;
    private OnCloseListener onCloseListener;
    /**
     * 空闲时间
     */
    private static final Long MILLISINFUTURE = (long) 61 * 1000;
    /**
     * 倒计时间隔
     */
    private static final Long COUNTDOWNINTERVAL = (long) 1 * 1000;


    private CountDownTimer countTimer;
    private boolean isShow = false;

    public ManagerVerificationDialog(@NonNull Context context, @StyleRes int themeResId, OnCloseListener onCloseListener) {
        super(context, themeResId);
        this.onCloseListener = onCloseListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.verification_layout);
        ButterKnife.bind(this);

        String uuid = PhoneUtil.getUUID(getContext());
        QRCodeLoginInfo info = new QRCodeLoginInfo(uuid);
        Gson mGson = new Gson();
        String jsonStr = mGson.toJson(info);
        Bitmap bmQR = ZXingUtils.createQRImage(jsonStr, 150, 150);
        Bitmap bmLogo = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.qr_logo);
        Bitmap bitmap = ZXingUtils.addLogo(bmQR, bmLogo);
        ivCode.setImageBitmap(bitmap);

        //设置不可取消，点击其他区域不能取消
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initCountTimer();
        CardReaderUtil.init();
        ivCode.setVisibility(isShow ? View.VISIBLE : View.GONE);
        llOr.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    private void initCountTimer() {
        countTimer = new CountDownTimer(MILLISINFUTURE, COUNTDOWNINTERVAL) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvTime.setText(("请刷卡(" + (millisUntilFinished / 1000) + "s )"));
            }

            @Override
            public void onFinish() {
                if (onCloseListener != null)
                    onCloseListener.OnClose();
               dismiss();
            }
        };
        countTimer.start();

    }

    @OnClick({R.id.ll_cancle})
    public void onViewClicked(View view) {
        if (onCloseListener != null)
            onCloseListener.OnClose();
        if(countTimer!=null)
            countTimer.cancel();
        cancel();

    }

    public void setShowQRCode(Boolean showQRCode) {
        this.isShow = showQRCode;
    }


    @Override
    public void cancel() {
        super.cancel();
        CardReaderUtil.onDestory();
    }

    public interface OnCloseListener {
        void OnClose();
    }
}
